package com.wfit.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wfit.constant.MessageConstant;
import com.wfit.context.UserContext;
import com.wfit.dto.CommodityDto;
import com.wfit.dto.CommodityListDto;
import com.wfit.dto.CommodityUpdateDto;
import com.wfit.dto.PageDTO;
import com.wfit.mapper.CateMapper;
import com.wfit.mapper.CommodityMapper;
import com.wfit.po.Cate;
import com.wfit.po.Commodity;
import com.wfit.result.Result;
import com.wfit.service.CartService;
import com.wfit.service.CateService;
import com.wfit.service.CommodityService;
import com.wfit.service.UploadService;
import com.wfit.vo.CommodityBusinessVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;

@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity> implements CommodityService {
    @Autowired
    private CateMapper cateMapper;
    @Override
    public Result list(CommodityListDto dto) {
        Page<Commodity> page = dto.toMpPageDefaultSortByUpdateTimeDesc();
        page = lambdaQuery()
                .eq(dto.getStatus() != null,Commodity::getStatus, dto.getStatus())
                .like(dto.getName() != null,Commodity::getName, dto.getName())
                .eq(dto.getSortId() != null,Commodity::getSortId, dto.getSortId())
                .eq(dto.getCSortId() != null,Commodity::getCSortId, dto.getCSortId())
                .page(page);
        PageDTO<CommodityBusinessVO> pageVO = PageDTO.of(page, new Function<Commodity, CommodityBusinessVO>() {
            @Override
            public CommodityBusinessVO apply(Commodity commodity) {
                String sortId = commodity.getSortId();
                Cate cate = cateMapper.selectById(sortId);
                CommodityBusinessVO commodityBusinessVO = new CommodityBusinessVO();
                BeanUtils.copyProperties(commodity,commodityBusinessVO);
                commodityBusinessVO.setSortName(cate.getName());
                if (commodity.getCSortId() != null ) {
                    commodityBusinessVO.setCSortName(cateMapper.selectById(commodity.getCSortId()).getName());
                }
                return commodityBusinessVO;
            }
        });

        return Result.success(pageVO);
    }
    @Autowired
    private UploadService uploadService;
    @Override
    public Result addCommodity(CommodityDto dto) throws IOException {
        if (Objects.isNull(dto)){
            return Result.error(MessageConstant.PARAM_IS_NULL);
        }
        Commodity commodity = new Commodity();
        BeanUtils.copyProperties(dto,commodity,"image","status");
        // 添加时间和操作人
        if (dto.getId() != null) {
            commodity.setUpdateTime(LocalDateTime.now());
            commodity.setUpdateUser(UserContext.getUser());
            if (dto.getImageFile() != null) {
                String upload = uploadService.upload(dto.getImageFile());
                commodity.setImage(upload);
            } else {
                commodity.setImage(dto.getImage());
            }
        } else {
            commodity.setSale(0);
            commodity.setCreateTime(LocalDateTime.now());
            commodity.setCreateUser(UserContext.getUser());
            String upload = uploadService.upload(dto.getImageFile());
            commodity.setImage(upload);
            commodity.setStatus("1");
        }

        commodity.setUpdateTime(LocalDateTime.now());
        commodity.setCSortId(dto.getCSortId());
        saveOrUpdate(commodity);
        return Result.success();
    }

    @Override
    public Commodity backOneCommodity(String commodityId) {
        Commodity byId = getById(commodityId);
        return byId;
    }

}
